package cn.itcast.service;

import cn.itcast.interfaces.MemberService;
import cn.itcast.mapper.MemberMapper;
import cn.itcast.pojo.Member;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import io.reactivex.Observable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/8/16 15:35
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 根据手机号查询会员信息
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findMemberByTelePhone(telephone);
    }

    /**
     * 添加会员
     * @param member
     */
    @Override
    public void add(Member member) {
        if(!ObjectUtils.isEmpty(member.getPassword())){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberMapper.add(member);
    }


    /**
     * 获取截至月份的会员数量
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(months) && months.size()>0){
            /*for (String month : months) {
                month=month+".31";
               Integer memberCount = memberMapper.findMemberCountBeforeDate(month);
                list.add(memberCount);
            }*/
            //Rxjava遍历
            Observable.fromIterable(months).subscribe(month -> {
                month=month+".31";
                Integer memberCount = memberMapper.findMemberCountBeforeDate(month);
                list.add(memberCount);
            });
        }
        return list;
    }
}
